<?php

namespace app\common\model\goods;

use app\common\model\Cart;
use app\common\model\Category;
use think\Model;
use traits\model\SoftDelete;

class Goods extends Model
{

    use SoftDelete;



    // 表名
    protected $name = 'goods';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'integer';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';

    // 追加属性
    protected $append = [
        'display_text',
        'state_text',
        'status_text',
        'preferential_text',
        'image_str',
        'images_array',
    ];

    public function getDisplayList()
    {
        return ['1' => __('正常'), '2' => __('展示')];
    }

    public function getStateList()
    {
        return ['1' => __('State 1'), '2' => __('State 2')];
    }
    public function getPreferentialList()
    {
        return ['0' => __('否'), '1' => __('是')];
    }
    public function getStatusList()
    {
        return ['0' => __('Status 0'), '1' => __('Status 1')];
    }
    public function getDisplayTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['display']) ? $data['display'] : '');
        $list = $this->getDisplayList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public function getPreferentialTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['preferential']) ? $data['preferential'] : '');
        $list = $this->getPreferentialList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function getStateTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['state']) ? $data['state'] : '');
        $list = $this->getStateList();
        return isset($list[$value]) ? $list[$value] : '';
    }
    public static function getCartNum($goods_id, $user_id)
    {
        return Cart::where('goods_id', $goods_id)->where('user_id', $user_id)->value('num') ?? 0;
    }

    public function getStatusTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['status']) ? $data['status'] : '');
        $list = $this->getStatusList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function category()
    {
        return $this->belongsTo('app\common\model\Category', 'category_id', 'id', [], 'LEFT')->setEagerlyType(0);
    }
    public function getImageStrAttr($value, $data)
    {
        return $data['image'] ? cdnurl($data['image'], true) : '';
    }
    public function getImagesArrayAttr($value, $data)
    {
        $arr = explode(',', $data['images']);
        foreach ($arr as &$v) {
            $v = cdnurl($v, true);
        }
        return $arr;
    }

    /**
     * 读取商品列表
     * @param int $cate_id   指定类型
     * @param string $keywords 指定状态
     * @return array
     */
    public static function getGoodsList(int $cate_id, $keywords = null, $state = null)
    {

        $list = self::where(function ($query) use ($cate_id, $keywords, $state) {
            if ($state == 1) {
                if (!is_null($state)) {
                    $query->where('state', '1');
                }
            } else {
                if (!is_null($cate_id) && $cate_id) {
                    $query->where('category_id', '=', $cate_id);
                }
                if (!is_null($keywords)) {
                    $query->whereLike('title', '%' . $keywords . '%');
                }
            }
        })->where('status','1')->paginate();
        return $list;
    }
    /**
     * 增销减库
     * @param int $goods_id   商品id
     * @param int $num 数量
     * @return bool
     */
    public static function reduceInventory($goods_id, $num)
    {
        $stock = self::where('id', $goods_id)->value('stock');
        if ($stock < $num) {
            exception('库存不足');
        }
        return self::where('id', $goods_id)->dec('stock', $num)->inc('sales', $num)->update();
    }
    /**
     * 减销增库
     * @param int $goods_id   商品id
     * @param int $num 数量
     * @return bool
     */
    public static function stockUp($goods_id, $num)
    {
        return self::where('id', $goods_id)->inc('stock', $num)->dec('sales', $num)->update();
    }
}
